package com.dlx.acl.controller.api;

import com.dlx.acl.service.UserService;
import com.dlx.common.result.R;
import com.dlx.vo.acl.UserRegisterVo;
import com.dlx.vo.acl.UserUpdateVo;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <h3>smms_parent</h3>
 *
 * @author nglon
 * @description <p>用户api模块</p>
 * @date 2021-11-25 20:12
 **/
@RestController
@RequestMapping("/api/acl/user")
public class UserApiController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserService userService;

    @PostMapping("register")
    public R register(@RequestBody UserRegisterVo userRegisterVo) {
        String code;
        try {
            code = redisTemplate.opsForValue().get(userRegisterVo.getMobile());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("短信验证码失效,请重新发送");
        }
        if (code == null) {
            return R.error().message("短信验证码失效,请重新发送");
        }
        if (!code.equals(userRegisterVo.getCode())) {
            return R.error().message("短信验证码错误");
        }
        Boolean flag = userService.register(userRegisterVo);
        return flag ? R.ok().message("注册成功") : R.error().message("注册失败");
    }

    @GlobalTransactional
    @PostMapping("update")
    public R update(@RequestBody UserUpdateVo userUpdateVo) {
        Boolean flag = this.userService.updateUserInfo(userUpdateVo);
        return flag ? R.ok().message("修改成功") : R.error().message("修改失败");
    }
}
